package sxkyxt.kjyxgs.cn.ishop_gather_uploading_android;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.telephony.PhoneNumberUtils;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.Window;
import android.view.inputmethod.InputMethodManager;
import android.widget.Toast;


import java.util.List;


public abstract class BaseActivity extends Activity implements DialogMaker.DialogCallBack {

    protected Dialog dialog;
    // 当前屏幕的高宽
    public static int screenW = 0;
    public static int screenH = 0;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        ActivityStack.getInstance().addActivity(this);

        DisplayMetrics dm = getResources().getDisplayMetrics();
        screenW = dm.widthPixels;
        screenH = dm.heightPixels;
    }


//    protected abstract void initParams();
//
//    @Override
//    protected void onResume() {
//        super.onResume();
//        if (isCreate) {
//            isCreate = false;
//            initParams();
//        }
//    }

    @Override
    protected synchronized void onDestroy() {
//        dismissDialog();
        ActivityStack.getInstance().removeActivity(this);
        super.onDestroy();
    }

    protected void showToast(String msg) {
        Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
    }

    protected void intoActivity(Class<?> cls) {
        Intent into = new Intent();
        into.setClass(this, cls);
        startActivity(into);
    }

    /**
     * 等待对话框
     *
     * @param msg
     * @param isCanCancelabel
     * @param tag
     * @return
     */
    public Dialog showWaitDialog(String msg, boolean isCanCancelabel, Object tag) {
        if (null == dialog || !dialog.isShowing()) {
            dialog = DialogMaker.showCommenWaitDialog(this, msg, this, isCanCancelabel, tag);
        }
        return dialog;
    }

    /**
     * 弹出对话框
     *
     * @author blue
     */
    public Dialog showAlertDialog(String title, String msg, String[] btns, boolean isCanCancelabel, final boolean isDismissAfterClickBtn, Object tag) {
        if (null == dialog || !dialog.isShowing()) {
            dialog = DialogMaker.showCommonAlertDialog(this, title, msg, btns, this, isCanCancelabel, isDismissAfterClickBtn, tag);
        }
        return dialog;
    }

    @Override
    public void onButtonClicked(Dialog dialog, int position, Object tag) {
    }

    @Override
    public void onCancelDialog(Dialog dialog, Object tag) {
    }

    /**
     * 关闭对话框
     *
     * @author blue
     */
    public void dismissDialog() {
        if (null != dialog && dialog.isShowing()) {
            dialog.dismiss();
        }
    }

    //影藏软键盘
    public static void hideKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    }

    //显示软键盘
    public static void showKeyboard(Context context, View view) {
        InputMethodManager imm = (InputMethodManager) context
                .getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.showSoftInput(view, 0);
    }

    /**
     * 直接调用短信接口发短信
     * @param phoneNumber
     * @param message
     */
    public  void sendSMS(String phoneNumber,String message){
        //获取短信管理器
        android.telephony.SmsManager smsManager = android.telephony.SmsManager.getDefault();
        //拆分短信内容（手机短信长度限制）
        //// TODO: 2017/4/25  短信长度限制
        List<String> divideContents = smsManager.divideMessage(message);
        for (String text : divideContents) {
            smsManager.sendTextMessage(phoneNumber, null, text,null, null);
        }
    }
    /**
     * 调起系统发短信功能
     *
     * @param phoneNumber
     * @param message
     */
    public void doSendSMSTo(String phoneNumber, String message)   throws  Exception {

        if (PhoneNumberUtils.isGlobalPhoneNumber(phoneNumber)) {
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + phoneNumber));
            intent.putExtra("sms_body", message);
            intent.setPackage("net.m56.ckkj.watermeters");
            startActivity(intent);
        }else{
           Toast.makeText(getApplicationContext(), R.string.mphone_not,Toast.LENGTH_SHORT).show();
        }

    }
}
